/**
  * 充值记录
  */
module game {

    export class AddMoneyJiLuMediator extends BaseMediator {
        public static NAME: string = "AddMoneyJiLuMediator";
        public constructor(viewComponent: any = null) {
            super(AddMoneyJiLuMediator.NAME, viewComponent);
        }

        public listNotificationInterests(): Array<any> {
            return [
                PanelNotify.HALL.STAGE_RESIZE,
                PanelNotify.HALL.OPEN_ADD_MONEY_JILU,
                PanelNotify.HALL.CLOSE_ADD_MONEY_JILU
            ];
        }
        public handleNotification(notification: puremvc.INotification): void {
            switch (notification.getName()) {
                case PanelNotify.HALL.STAGE_RESIZE:
                    if (this.panel) {
                        this.panel.width = GameConfig.curWidth()
                        this.panel.height = GameConfig.curHeight()
                    }
                    break;
                case PanelNotify.HALL.OPEN_ADD_MONEY_JILU: {
                    this.panel = null;
                    this.panel = new CZ_JiLuPanel();
                    this.showUI(this.panel, 2, 1, true);
                    break;
                }
                case PanelNotify.HALL.CLOSE_ADD_MONEY_JILU: {
                    this.closePanel(0);
                    break;
                }
            }
        }
        private panel: CZ_JiLuPanel;
        /**
         * 初始化面板ui
         */
        public initUI(): void {
            this.panel.closeBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.closeButtonClick, this)
        }

        /**
         * 初始化面板数据
         */
        //线上线下分别查询，都查到了再合并数据
        private xianshang = false
        private xianxia = false
        private dataList: Array<Object> = [];
        public initData(): void {
            this.xianshang = false
            this.xianxia = false
            this.dataList = [];

            var time = Global.getCurrTimeLong()
            var signature = Global.getSynLoginKey(time)
            signature = signature.toUpperCase()
            //查询线上
            var request = new egret.HttpRequest();
            request.responseType = egret.HttpResponseType.TEXT;
            request.open(InstructionsEnum.IP_API + "/Payment/RechargeRecord?userid=" + GameVO.getVo().dwUserID + "&id=" + GameVO.getVo().dwUserID + "&time=" + time + "&signature=" + signature + "&pageSize=" + 50 + "&pageIndex=" + 1, egret.HttpMethod.POST);
            request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            request.addEventListener(egret.Event.COMPLETE, (event: egret.Event) => {
                EffectUtils.hideWaritPanel()
                try {
                    var request = <egret.HttpRequest>event.currentTarget;
                    var obj = JSON.parse(request.response)
                    if (obj.code == 0) {
                        var List: Array<Object> = obj["List"];
                        this.dataList = this.dataList.concat(List)
                        this.xianshang = true;
                        this.showData()
                    } else {
                        Global.tipShow(obj.msg)
                    }
                } catch (err) {
                    EffectUtils.textFadeAway("数据包异常，无法解析!")
                }
            }, this);
            request.addEventListener(egret.IOErrorEvent.IO_ERROR, () => {
                EffectUtils.hideWaritPanel()
                Global.tipShow("网络请求失败，请稍后重试!")
            }, this);
            EffectUtils.showWaritPanel()
            request.send();
            //查询线下
            var request = new egret.HttpRequest();
            request.responseType = egret.HttpResponseType.TEXT;
            request.open(InstructionsEnum.IP_API + "/Payment/InquireOrder?userid=" + GameVO.getVo().dwUserID + "&id=" + GameVO.getVo().dwUserID + "&time=" + time + "&signature=" + signature + "&pageSize=" + 50 + "&pageIndex=" + 1, egret.HttpMethod.POST);
            request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            request.addEventListener(egret.Event.COMPLETE, (event: egret.Event) => {
                EffectUtils.hideWaritPanel()
                try {
                    var request = <egret.HttpRequest>event.currentTarget;
                    var obj = JSON.parse(request.response)
                    if (obj.code == 0) {
                        var List: Array<Object> = obj["List"];
                        this.dataList = this.dataList.concat(List)
                        this.xianxia = true;
                        this.showData()
                    } else {
                        Global.tipShow(obj.msg)
                    }
                } catch (err) {
                    EffectUtils.textFadeAway("数据包异常，无法解析!")
                }
            }, this);
            request.addEventListener(egret.IOErrorEvent.IO_ERROR, () => {
                EffectUtils.hideWaritPanel()
                Global.tipShow("网络请求失败，请稍后重试!")
            }, this);
            EffectUtils.showWaritPanel()
            request.send();
        }

        private showData() {
            //先把所有订单生成时间转换成时间戳，然后按照时间戳排序
            if (!this.xianxia) {
                return
            }
            if (!this.xianshang) {
                return
            }
            var arr = this.dataList
            var leng = arr.length;
            for (var i = 0; i < leng; i++) {
                var data: Object = arr[i]
                var time;
                if (data["CreateTime"]) {
                    time = data["CreateTime"]
                    data["type"] = 1//线下订单
                } else {
                    time = data["PayTime"]
                    data["type"] = 2//线上
                }
                var date = new Date(time);
                data["time"] = date.getTime();
            }
            //按时间排序
            for (var i = 0; i < leng - 1; i++) {
                for (var j = 0; j < leng - 1 - i; j++) {
                    if (arr[j]["time"] < arr[j + 1]["time"]) {
                        var temp = arr[j];
                        arr[j] = arr[j + 1];
                        arr[j + 1] = temp;
                    }
                }
            }
            for (var i = 0; i < leng; i++) {
                this.panel.dataGroup.addChild(new CZ_jilvtiaoPanel(arr[i]))
            }
        }
        private closeButtonClick(event: egret.TouchEvent): void {
            SoundManage.startAnniuBtnLoad();
            this.closePanel(1);
        }

    }
}
